/**
 * 
 */
package com.williamoneil.charts.client.widget;

import java.util.ArrayList;
import java.util.List;

import com.google.gwt.canvas.dom.client.Context2d;
import com.williamoneil.charts.client.data.ChartBoundsRect;
import com.williamoneil.charts.client.data.ChartElementsData;
import com.williamoneil.charts.client.data.DateAxis;
import com.williamoneil.charts.client.data.LineData;
import com.williamoneil.charts.client.data.PriceAxis;
import com.williamoneil.charts.client.util.ColorEntityType;
import com.williamoneil.charts.client.util.Colors;
import com.williamoneil.charts.shared.FinancialData;

/**
 * @author gudipati
 *
 */
public class EPSLineLayer extends ChartOverlay {
	
	private static final double END_ANGLE = 2 * Math.PI;
	
	private int multiple = 20;
	
	public EPSLineLayer(final ChartBoundsRect chartBounds) {
		super(chartBounds);
	}
	
	/**
	 * @return the multiple
	 */
	public int getMultiple() {
		return multiple;
	}

	/**
	 * @param multiple the multiple to set
	 */
	public void setMultiple(int multiple) {
		this.multiple = multiple;
	}

	/* (non-Javadoc)
	 * @see com.williamoneil.charts.client.widget.ChartOverlay#drawOverlay()
	 */
	@Override
	public void drawOverlay() {
		final ChartElementsData elements = super.getChartElements();
		if(elements == null) {
			return;
		}
		
		drawEPSLine();
	}
	
	private void drawEPSLine() {
		final ChartElementsData elements = super.getChartElements();
		if(elements == null || elements.getFinancials() == null) {
			return;
		}
		
		final DateAxis dateAxis = elements.getDateAxis();
		final ChartBoundsRect bounds = super.getChartBoundsRect();
		final PriceAxis priceAxis = elements.getPriceAxis();
		final Context2d context = super.getGraphicsContext();

		final List<LineData> lineList = new ArrayList<LineData>();
		for(final FinancialData aData : elements.getFinancials()) {
			if(aData == null || aData.getDate() == null || aData.getEpsT4Q() == null) {
				continue;
			}
			
			final int x = dateAxis.getX(aData.getDate());
			if(!bounds.isXWithinBounds(x)) {
				break;
			}
			
			int y = priceAxis.getY(aData.getEpsT4Q().doubleValue() * multiple, bounds);
			
			if(!bounds.isYWithinBounds(y)) {
				continue;
			}
			
			final LineData line = new LineData(x, y);
			lineList.add(line);
		}
		
		if(lineList.size() > 0) {
			context.beginPath();
			final String color = Colors.getColorTheme().getColor(ColorEntityType.EPS_LINE);
			context.setStrokeStyle(color);
			context.setFillStyle(color);

			context.moveTo(lineList.get(0).getX(), lineList.get(0).getY());
			for(int i=0;i<lineList.size();i++) {
				final LineData aData = lineList.get(i);
				context.lineTo(aData.getX(), aData.getY());
			}
			context.stroke();
			
			context.moveTo(lineList.get(0).getX(), lineList.get(0).getY());
			for(int i=0;i<lineList.size();i++) {
				final LineData aData = lineList.get(i);
				context.arc(aData.getX(), aData.getY(), 3, 0, END_ANGLE);
			}
			context.stroke();
		}
	}
}